package com.huluobo.a2209mdmdemo.views

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.google.gson.Gson
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.RoutePath
import com.hyphenate.EMValueCallBack
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMMessage
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/3/8.
 */

data class ContactBean(val username: String, val firstLetter: Char, val isShowFirstLetter: Boolean)

@Composable
fun ContactPage(navHostController: NavHostController) {
    val list = remember {
        SnapshotStateList<ContactBean>()
    }


    LaunchedEffect(key1 = "", block = {
        launch {
            EMClient.getInstance().contactManager()
                .aysncGetAllContactsFromServer(object : EMValueCallBack<List<String>> {
                    override fun onSuccess(value: List<String>?) {
                        if (!value.isNullOrEmpty()) {
                            //循环所有的联系人
                            for ((index, contact) in value.withIndex()) {
                                var isShow = false
                                //当联系人列表下标为0的时候,一定显示首字符
                                //当对比当前字符与上一个字符首字符不同的时候,也显示首字符
                                if (index == 0 || value[index].first() != value[index - 1].first()) {
                                    isShow = true
                                }
                                list.add(ContactBean(contact, contact.first().toUpperCase(), isShow))
                            }
                        }
                    }

                    override fun onError(error: Int, errorMsg: String?) {
                        Log.i("TagA", "获取联系人失败:$error")
                    }
                })
        }
    })

    Column(modifier = Modifier.padding(20.dp)) {
        BaseTitle(title = "联系人", navHostController = navHostController)
        Spacer(modifier = Modifier.height(20.dp))
        LazyColumn(content = {
            items(list) {
                ContactItem(contactBean = it, navHostController = navHostController)
            }
        })
    }
}

@Composable
fun ContactItem(contactBean: ContactBean, navHostController: NavHostController) {
    Column(
        Modifier
            .fillMaxWidth()
            .padding(10.dp)
            .clickable {
                val chatBean = ChatBean(contactBean.username, EMMessage.ChatType.Chat)
                val json = Gson().toJson(chatBean)
                navHostController.navigate(RoutePath.CHAT + "/$json")
            }, verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        if (contactBean.isShowFirstLetter) {
            Text(
                text = contactBean.firstLetter.toString(),
                Modifier
                    .background(Color.LightGray)
                    .padding(start = 10.dp)
                    .fillMaxWidth()
            )
        }
        Text(
            text = contactBean.username,
            Modifier
                .padding(start = 20.dp)
                .fillMaxWidth()
        )
    }
}